﻿using Newtonsoft.Json;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using SelfService.XinXing.Models;
using TT;
using WalkingTec.Mvvm.Core;

namespace SelfService.XinXing
{
    public class ScreenQueue : IScreenQueueService
    {
        private readonly WTMContext _wtm;
        private static readonly object Locker = new();

        public ScreenQueue(WTMContext wtm)
        {
            _wtm = wtm;
        }
        /// <summary>
        /// 接口使用了并发锁
        /// 如果遇到性能问题请改成多线程队列处理
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public object Process(string body)
        {
            lock (Locker)
            {
                try
                {
                    if (body.TryJsonParse(out ScreenQueueValidate data))
                    {
                        SysDoctor doctor = default;
                        //保存诊室
                        var room = _wtm.DC.Set<SysRoom>().FirstOrDefault(e => e.HisId == data.RoomId);
                        if (room == null)
                        {
                            room = new SysRoom();
                            room.Name = data.RoomName;
                            room.HisId = data.RoomId;
                            room.Code = "";
                            room.DepartmentId = "";
                            room.DepartmentCode = data.DeptCode;
                            room.DepartmentName = data.DeptName;
                            room.ID = Guid.NewGuid();
                            room.CreateTime = DateTime.Now;
                            room.CreateBy = "大屏队列";
                            room.UpdateTime = DateTime.Now;
                            room.UpdateBy = "大屏队列";
                            _wtm.DC.Set<SysRoom>().Add(room);
                            _wtm.DC.SaveChanges();
                        }

                        //保存医生
                        if (!data.DoctorNo.IsNullOrWhiteSpace() && data.DoctorNo != "***")
                        {
                            doctor = _wtm.DC.Set<SysDoctor>().FirstOrDefault(e => e.Code == data.DoctorNo);
                            if (doctor == null)
                            {
                                doctor = new SysDoctor();
                                doctor.RoomId = room.ID;
                                doctor.Code = data.DoctorNo;
                                doctor.Name = data.DoctorName;
                                doctor.DoctorLevel = data.DoctorLevel;
                                doctor.Info = data.DoctorInfo;
                                doctor.ID = Guid.NewGuid();
                                doctor.CreateTime = DateTime.Now;
                                doctor.CreateBy = "大屏队列";
                                doctor.UpdateTime = DateTime.Now;
                                doctor.UpdateBy = "大屏队列";
                                _wtm.DC.Set<SysDoctor>().Add(doctor);
                                _wtm.DC.SaveChanges();
                            }
                        }

                        var queue = new SysScreenQueue();
                        queue.ID = Guid.NewGuid();
                        queue.DoctorId = doctor?.ID;
                        queue.RoomId = room.ID;
                        queue.CreateTime = DateTime.Now;
                        queue.Info = System.Text.Json.JsonSerializer.Serialize(data);
                        _wtm.DC.Set<SysScreenQueue>().Add(queue);
                        _wtm.DC.SaveChanges();
                        return new ReservationResult<object>();
                    }
                    _wtm.DoLog($"数据解析失败:{body}", ActionLogTypesEnum.Exception, "", "新星大屏数据接收接口");
                    return new ReservationResult<object>()
                    {
                        Code = "-1",
                        Message = "数据解析失败"
                    };

                }
                catch (Exception e)
                {
                    _wtm.DoLog($"数据接收异常:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception, "", "新星大屏数据接收接口");
                    return new ReservationResult<object>()
                    {
                        Code = "-1",
                        Message = "数据接收异常"
                    };
                }
            }
        }
    }
}
